using UnityEngine;
using System.Collections;

public class GamePlayer : MonoBehaviour {
	
	public float velLocomocaoVertical, velLocomocaoHorizontal;
	public float TopLimit, BottonLimit;
	public float alturaPulo, alturaSmoth;
	
	public GameObject Player;
	public GameObject Projetil;	
	
	public float repulseDistence;
	
	bool pulando;
	bool descendo;
	float InconstantAlturaPulo, inconstantAlturaZero;
	
	bool repulse;
	float tempRepulseDistence;
	void Start () {
	
	}
	void Update () {
	
		if(!pulando){
			if(Input.GetKey("up") && Player.transform.position.y < TopLimit)
			{
				Player.transform.Translate(new Vector3(0,velLocomocaoVertical,0)*Time.deltaTime);	
			}
			if(Input.GetKey("down") && Player.transform.position.y > BottonLimit)
			{
				Player.transform.Translate(new Vector3(0, -velLocomocaoVertical,0)*Time.deltaTime);
			}
		}
		if(Input.GetKey("left"))
		{
			Player.transform.localEulerAngles = new Vector3(0,180,0);
			Player.transform.Translate(new Vector3(velLocomocaoHorizontal,0,0)*Time.deltaTime);	
		}
		if(Input.GetKey("right"))
		{
			Player.transform.localEulerAngles = new Vector3(0,0,0);
			Player.transform.Translate(new Vector3(velLocomocaoHorizontal,0,0)*Time.deltaTime);
		}
		
		if(Input.GetButtonDown("Fire1"))
		{
			GameObject TempProjetil = Instantiate(Projetil, new Vector3(Player.transform.position.x, Player.transform.position.y,Player.transform.position.z), new Quaternion(0,transform.localEulerAngles.y,0,0)) as GameObject;
			Player.transform.Translate(new Vector3(-repulseDistence,0,0)*Time.deltaTime);
		}
		if(pulando)
		{
			Player.transform.position = Vector3.Lerp(Player.transform.position, new Vector3(Player.transform.position.x, InconstantAlturaPulo, Player.transform.position.z), alturaSmoth);
			if( Mathf.Abs(Player.transform.position.y- InconstantAlturaPulo) <= 0.2)
			{
				if(!descendo)
				{
					InconstantAlturaPulo = inconstantAlturaZero - 0.2f;
					descendo = true;
				}else
				{
					pulando = false;
					descendo = false;
				}
			}

		}
		if(Input.GetKeyDown("space") && !pulando && !descendo)
		{
			pulando = true;
			InconstantAlturaPulo = Player.transform.position.y + alturaPulo;
			inconstantAlturaZero = Player.transform.position.y;
		}
	}
}